package com.pingao.mp.modular.sys.controller;

import com.pingao.mp.modular.sys.entity.Menu;
import com.pingao.mp.modular.sys.entity.User;
import com.pingao.mp.modular.sys.service.MenuService;
import com.pingao.mp.modular.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping({"/sys","/sys/index"})
public class IndexController {

    private final UserService userService;
    private final MenuService menuService;

    @Autowired
    public IndexController(UserService userService, MenuService menuService) {
        this.userService = userService;
        this.menuService = menuService;
    }

    @GetMapping({"","/index"})
    @RequiresUser
    public String index(ModelMap map){
        User sessionUser = userService.getSessionUser();
        if(sessionUser == null){
            return "sys/user/login";
        }
        List<Menu> loginMenu = menuService.getLoginMenu();
        map.addAttribute("loginMenu",loginMenu);
        return "sys/index/index";
    }

    @GetMapping("/desktop")
    @RequiresUser
    public String desktop() {
        return "sys/index/desktop";
    }
}
